//
//  MainCountDownView.m
//  ZITHER
//
//  Created by weixin on 2018/11/29.
//  Copyright © 2018 weixn. All rights reserved.
//

#import "MainCountDownView.h"
#import "UIView+ArrangeSubview.h"
#import "CountDown.h"

@interface MainCountDownView()
@property (strong, nonatomic)  UILabel *dayLabel;
@property (strong, nonatomic)  UILabel *hourLabel;
@property (strong, nonatomic)  UILabel *minuteLabel;
@property (strong, nonatomic)  UILabel *secondLabel;

@property (strong, nonatomic)  UILabel *timeLab;

@property (strong, nonatomic)  UIView *contentView;

@property (strong, nonatomic)  CountDown *countDownForBtn;

@property (strong, nonatomic)  CountDown *countDownForLabel;
@end

@implementation MainCountDownView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = [UIColor randomColor];
        
        _countDownForLabel = [[CountDown alloc] init];
        _countDownForBtn = [[CountDown alloc] init];
        
        ///方法一倒计时测试
//        long long startLongLong = 1467713971000;
//        long long finishLongLong = 1467722006200;
//        [self startLongLongStartStamp:startLongLong longlongFinishStamp:finishLongLong];
        
        
//        NSDateFormatter* formater = [[NSDateFormatter alloc] init];
//        [formater setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
//        NSDate* startDate = [formater dateFromString:@"2015-12-01 19:23:20"];
//        NSDate* finishDate = [formater dateFromString:@"2015-12-31 19:24:20"];
//        [self startWithStartDate:startDate finishDate:finishDate];
        
        //    60s的倒计时
        NSTimeInterval aMinutes = 24*60*60*2;
        //    1个小时的倒计时
        //    NSTimeInterval anHour = 60*60;
        //     1天的倒计时
        //    NSTimeInterval aDay = 24*60*60;
        [self startWithStartDate:[NSDate date] finishDate:[NSDate dateWithTimeIntervalSinceNow:aMinutes]];
        
        [self setUI];
    }
    return self;
}


-(void)setUI{
    CGFloat label_width = 40;
    CGFloat label_Height = 40;
    
    self.contentView = [UIView new];
    self.contentView.backgroundColor = [UIColor magentaColor];
    [self addSubview:self.contentView];
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(4*label_width, label_Height));
        make.center.equalTo(self);
    }];
    
    self.timeLab = [UILabel new];
    self.timeLab.textAlignment = NSTextAlignmentCenter;
    self.timeLab.backgroundColor = [UIColor cyanColor];
    self.timeLab.font = [UIFont fontWithName:@"PingFangSC-Medium" size:40];
    [self.contentView addSubview:self.timeLab];
    
    self.dayLabel = [UILabel new];
    self.dayLabel.textAlignment = NSTextAlignmentCenter;
    self.dayLabel.backgroundColor = [UIColor cyanColor];
    self.dayLabel.font = [UIFont systemFontOfSize:15];
    [self.contentView addSubview:self.dayLabel];
    
    
    self.hourLabel = [UILabel new];
    self.hourLabel.textAlignment = NSTextAlignmentCenter;
    self.hourLabel.font = [UIFont systemFontOfSize:15];
    self.hourLabel.backgroundColor = [UIColor greenColor];
    [self.contentView addSubview:self.hourLabel];
    
    
    self.minuteLabel = [UILabel new];
    self.minuteLabel.textAlignment = NSTextAlignmentCenter;
    self.minuteLabel.font = [UIFont systemFontOfSize:15];
    self.minuteLabel.backgroundColor = [UIColor redColor];
    [self.contentView addSubview:self.minuteLabel];
    
    
    self.secondLabel = [UILabel new];
    self.secondLabel.textAlignment = NSTextAlignmentCenter;
    self.secondLabel.font = [UIFont systemFontOfSize:15];
    self.secondLabel.backgroundColor = [UIColor orangeColor];
    [self.contentView addSubview:self.secondLabel];
    
    self.timeLab.text = @"08:10:20";
    self.timeLab.textColor = baseColorTheme;
    self.timeLab.backgroundColor = [UIColor whiteColor];
    [self.timeLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.mas_centerY).offset(0);
        make.left.right.equalTo(self).offset(0);
    }];
    
//    [self.dayLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.centerY.equalTo(@[self.hourLabel,self.minuteLabel,self.secondLabel]);
//        make.top.equalTo(self.contentView);
//        make.width.mas_equalTo(label_width);
//        make.height.equalTo(self.contentView);
//
//    }];
//    [self.hourLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.size.equalTo(self.dayLabel);
//    }];
//    [self.minuteLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.size.equalTo(self.dayLabel);
//    }];
//    [self.secondLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.size.equalTo(self.dayLabel);
//    }];
//
//    [self.contentView arrangeSubviewWithSpacingHorizontally:@[self.dayLabel,self.hourLabel,self.minuteLabel,self.secondLabel]];
    
    
    
    
 
}

/**
 *  获取当天的年月日的字符串
 *  这里测试用
 *  @return 格式为年-月-日
 */
-(NSString *)getyyyymmdd{
    NSDate *now = [NSDate date];
    NSDateFormatter *formatDay = [[NSDateFormatter alloc] init];
    formatDay.dateFormat = @"yyyy-MM-dd";
    NSString *dayStr = [formatDay stringFromDate:now];
    return dayStr;
}


-(void)nextPage:(UIButton *)sender{
    UIViewController *aVC = [[UIViewController alloc]init];
    aVC.view.backgroundColor = [UIColor whiteColor];
    [self.getNav pushViewController:aVC animated:YES];
}
-(void)fetchCoder:(UIButton *)sender{
    //    60s的倒计时
    NSTimeInterval aMinutes = 60;
    //    1个小时的倒计时
    //    NSTimeInterval anHour = 60*60;
    //     1天的倒计时
    //    NSTimeInterval aDay = 24*60*60;
    [self startWithStartDate:[NSDate date] finishDate:[NSDate dateWithTimeIntervalSinceNow:aMinutes]];
}
#pragma mark
#pragma mark viewDidLoad


-(void)refreshUIDay:(NSInteger)day hour:(NSInteger)hour minute:(NSInteger)minute second:(NSInteger)second{
    
    NSString *str = @"";
    
    if (day==0) {
        if (hour<10&&hour) {
            str = [NSString stringWithFormat:@"0%ld:",(long)hour];
        }else{
            str = [NSString stringWithFormat:@"%ld:",(long)hour];
            }
    }else{
        long longg =(long)hour+(long)day*24;
        if (hour<10&&hour) {
            str = [NSString stringWithFormat:@"0%ld:",longg];
        }else{
            str = [NSString stringWithFormat:@"%ld:",longg];
        }
    }

    if (minute<10) {
        str = [NSString stringWithFormat:@"%@0%ld:",str,(long)minute];

//        self.minuteLabel.text = [NSString stringWithFormat:@"0%ld:",(long)minute];
    }else{
        str = [NSString stringWithFormat:@"%@%ld:",str,(long)minute];

//        self.minuteLabel.text = [NSString stringWithFormat:@"%ld:",(long)minute];
    }
    
    if (second<10) {
        str = [NSString stringWithFormat:@"%@0%ld",str,(long)second];
//        self.secondLabel.text = [NSString stringWithFormat:@"0%ld",(long)second];
    }else{
        str = [NSString stringWithFormat:@"%@%ld",str,(long)second];
//        self.secondLabel.text = [NSString stringWithFormat:@"%ld",(long)second];
    }
    self.timeLab.text = str;
}
///此方法用两个时间戳做参数进行倒计时
-(void)startLongLongStartStamp:(long long)strtLL longlongFinishStamp:(long long)finishLL{
    __weak __typeof(self) weakSelf= self;
    [_countDownForLabel countDownWithStratTimeStamp:strtLL finishTimeStamp:finishLL completeBlock:^(NSInteger day, NSInteger hour, NSInteger minute, NSInteger second) {
        NSLog(@"666");
        [weakSelf refreshUIDay:day hour:hour minute:minute second:second];
    }];
}
//此方法用两个NSDate对象做参数进行倒计时
-(void)startWithStartDate:(NSDate *)strtDate finishDate:(NSDate *)finishDate{
    __weak __typeof(self) weakSelf= self;
    [_countDownForBtn countDownWithStratDate:strtDate finishDate:finishDate completeBlock:^(NSInteger day, NSInteger hour, NSInteger minute, NSInteger second) {
        NSLog(@"second = %li",second);
        NSInteger totoalSecond =day*24*60*60+hour*60*60 + minute*60+second;
        [weakSelf refreshUIDay:day hour:hour minute:minute second:second];

        
    }];
}




@end
